/**
* 创建图的类
*/
class Dictionay {
  constructor () {
    this.items = {}
  }
  // 判断字典中是否存在某个key
  has (key) {
    return this.items.hasOwnProperty(key)
  }
  // 在字典中添加键值对
  Set (key, value) {
    this.items[key] = value
  }
  // 在字典中删除指定的key
  remove (key) {
    if (!this.has(key)) return false
    delete this.items[key]
  }
  // 获取指定key的value, 如果没有返回undefined
  get (key) {
    return this.has(key) ? this.items[key] : undefined
  }
  //获取所有的key
  keys () {
    return Object.keys(this.items)
  }
  // 获取所有的value
  values () {
    return Object.values(this.items)
  }
  // 获取字典中键值对格式
  size () {
    return Object.keys(this.items).length
  }
  //清空字典
  clear () {
    this.items = {}
  }
}
class Graph {
  constructor () {
    this.vertexes = [] // 存储顶点
    this.adjList = new Dictionay() // 存储边的信息
  }
  // 添加顶点
  addVertex (val) {
    // 添加点
    this.vertexes.push(val)
    // 添加点的关系 采用邻接矩阵法 结构用set
    this.adjList.Set(val, [])
  }
  // 添加边
  addEdge (val1, val2) {
    // 添加边需要传入两个顶点 因为边是两个顶点之间的边 边不可能自己存在
    this.adjList.get(val1).push(val2)
    this.adjList.get(val2).push(val1)
  }
  // 输出图结构
  toString () {
    let res = ''
    for (let i = 0; i < this.vertexes.length; i++) {
      res += this.vertexes[i] + "->"
      let adj = this.adjList.get(this.vertexes[i])
      for (let j = 0; j < adj.length; j++) {
        res += adj[j] + ""
      }
      res += "\n"
    }
    return res
  }
}
let graph = new Graph()
console.log(graph);
let myVertexes = ["A", "B", "C", "D", "E", "F", "G", "H", "I"]
for (let i = 0; i < myVertexes.length; i++) {
  graph.addVertex(myVertexes[i])
}
// 添加边
graph.addEdge('A', 'B');
graph.addEdge('A', 'C');
graph.addEdge('A', 'D');
graph.addEdge('C', 'D');
graph.addEdge('C', 'G');
graph.addEdge('D', 'G');
graph.addEdge('D', 'H');
graph.addEdge('B', 'E');
graph.addEdge('B', 'F');
graph.addEdge('E', 'I');
// 输出图结构
console.log(graph.toString())